<template>
  <div >
    <!-- 整租操作详情 -->
    <el-dialog title="信息" :visible.sync="openWarehouseLeaseOperationDetail" width="800px" append-to-body :destroy-on-close="true" :before-close="beforeClose">
        <el-form ref="formParams" :model="formParams" :rules="rules" label-width="140px" :inline="true">
        <el-row>
            <el-col :span="12">
            <el-form-item label="客户" prop="khmc" :rules="rules.khmc">
                <el-input
                    v-model="formParams.khmc"
                    :disabled="true"
                ></el-input>
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="仓库" prop="spckmc" :rules="rules.spckmc">
                <el-input
                    v-model="formParams.spckmc"
                    :disabled="true"
                />
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="所在系统" prop="khlx">
                <el-input
                v-model="formParams.khlx"
                :disabled="true"
                />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="天数" prop="days">
                <el-input
                v-model="getDays"
                :disabled="true"
                />
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="租仓开始日期" prop="date1">
              <el-date-picker
                v-model="formParams.date1"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="请输入租仓开始日期"
                :disabled="getAuditState"
                >
              </el-date-picker>
            </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="租仓结束日期" prop="date2">
                <el-date-picker
                  v-model="formParams.date2"
                  value-format="yyyy-MM-dd"
                  type="date"
                  placeholder="请输入租仓结束日期"
                  :disabled="getAuditState"
                  >
                </el-date-picker>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="使用面积（平方米）" prop="cwmj">
                <el-input
                v-model="formParams.cwmj"
                placeholder="请输入使用面积（平方米）"
                clearable
                :disabled="getAuditState"
                />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="最大容量上限(吨)" prop="ckrl">
                <el-input
                v-model="formParams.ckrl"
                placeholder="请输入最大容量上限(吨)"
                clearable
                :disabled="getAuditState"
                />
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="超仓单价(元/吨·天)" prop="ccdj">
                <el-input
                v-model="formParams.ccdj"
                placeholder="请输入超仓单价(元/吨·天)"
                clearable
                :disabled="getAuditState"
                />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="整租单价(元/平方米)" prop="ckdj">
                <el-input
                v-model="formParams.ckdj"
                placeholder="请输入整租单价(元/平方米)"
                clearable
                :disabled="getAuditState"
                />
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="整租金额(元)" prop="ckje">
                <el-input
                v-model="formParams.ckje"
                placeholder="请输入整租金额(元)"
                clearable
                :disabled="getAuditState"
                />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="联系人" prop="dzzy7">
                <el-input
                v-model="formParams.dzzy7"
                placeholder="请输入联系人"
                clearable
                :disabled="getAuditState"
                />
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="联系电话" prop="dzzy8">
                <el-input
                v-model="formParams.dzzy8"
                placeholder="请输入联系电话"
                clearable
                :disabled="getAuditState"
                />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="备注" prop="dzbz">
                <el-input
                v-model="formParams.dzbz"
                placeholder="请输入备注"
                clearable
                :disabled="getAuditState"
                />
            </el-form-item>
            </el-col>
        </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleAudit" v-show="!getAuditState" v-hasPermi="['zhcloud:ckzz:audit']">审核</el-button>
          <el-button @click="handleReject" v-show="getAuditState" v-hasPermi="['zhcloud:ckzz:reject']">驳回</el-button>
          <el-button type="primary" @click="handleEdit('formParams')" v-show="!getAuditState" v-hasPermi="['zhcloud:ckzz:modify']">修改</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { warehouseLeaseOperationModify, warehouseLeaseOperationReject, warehouseLeaseOperationAudit } from "@/api/storage/warehouseLease/warehouseLeaseOperation"
import { NUMBER_DIDGIT } from '@/utils/constant'
import { calculateDays } from '@/utils'


export default {
  name: "WarehouseLeaseOperationDetail",
  data() {
    return {
       // 表单校验
      rules: {
        spjs3: [
          { required: true, message: "拆分件数不能为空", trigger: "blur" },
          { required: true, validator: this.checkspjs3, trigger: "blur" }
        ]
      },
      //审核状态
      auditState:true,
      //弹窗状态
      openWarehouseLeaseOperationDetail: false,
      //表单数据
      formParams:{},
      submitParams: {
        bckld: "",
        ccdj: "",
        ckdj: "",
        ckje: "",
        ckrl: "",
        cwmj: "",
        date1: "",
        date2: "",
        days: "",
        dzbz: "",
        dzid: "",
        dzzy7: "",
        dzzy8: "",
        khlx: "",
        khmc: "",
        khmcid: "",
        spckid: ""
      },
    };
  },
  computed: {
    getDays() {
      const date1 = this.formParams.date1
      const date2 = this.formParams.date2
      return date1 && date2?calculateDays(date1, date2) + 1:""
    },
    getAuditState() {
      return this.auditState  == "未审核"?false:true
    }
  },
  watch:{
    'formParams.spjs': function(data) {
      if(data !== undefined) {
        if(data.indexOf('.')>0){
          data=data.slice(0,data.indexOf('.')+ (NUMBER_DIDGIT + 1))
        }
        this.formParams.spjs = data
      }
    },
  },
  methods: {
      /**驳回 */
    handleReject() {
      this.$modal.loading("数据正在驳回中，请稍等...")
      const submitParams = this.submitParams
      for(let i in submitParams) {
        submitParams[i] = this.formParams[i]
      }
      warehouseLeaseOperationReject(this.submitParams).then(response => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess('驳回成功！')
        this.close()
        this.$emit('refresh', true)
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    }, 
    /**审核 */
    handleAudit() {
      this.$modal.loading("数据正在审核中，请稍等...")
       const submitParams = this.submitParams
      for(let i in submitParams) {
        submitParams[i] = this.formParams[i]
      }
      warehouseLeaseOperationAudit(this.submitParams).then(response => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess('审核成功！')
        this.close()
        this.$emit('refresh', true)
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },
    /**修改 */
    handleEdit() {
      this.$modal.loading("数据正在修改中，请稍等...")
       const submitParams = this.submitParams
      for(let i in submitParams) {
        submitParams[i] = this.formParams[i]
      }
      warehouseLeaseOperationModify(this.submitParams).then(response => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess('修改成功！')
        this.close()
        this.$emit('refresh', true)
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },

    reset() {
      Object.assign(this.$data, this.$options.data())
    },
    /**关闭弹窗 */
    beforeClose(done) {
      this.close()
      done()
    },
    /**关闭窗口 */
    close() {
      this.changeWarehouseLeaseOperationDetail()
      this.reset()
    },
    /**改变窗口状态 */
    changeWarehouseLeaseOperationDetail() {
      this.openWarehouseLeaseOperationDetail = !this.openWarehouseLeaseOperationDetail;
    },

    /**打开弹窗 */
    open(data) {
      this.changeWarehouseLeaseOperationDetail()
      this.formParams = data
      this.$set(this.formParams,'date1',data.ssrqid)
      this.$set(this.formParams,'date2',data.ssrqidn)
      this.formParams.date2
      this.auditState = data.shzt
    },
  }
};
</script>
